package com.example.service;
import org.springframework.stereotype.Service;
import com.example.entity.Order;
import com.example.entity.OrderType;
import com.example.entity.Vehicle;

@Service
public class FareCalculator {
    // 自动驾驶车辆收费标准为经济型车辆收费标准的30%
    private static final double AUTONOMOUS_DISCOUNT = 0.3;
    
    // 不同类型车辆的抽成比例
    private static final double COMPANY_OWNED_COMMISSION_RATE = 0.3; // 公司车辆抽成30%
    private static final double DRIVER_OWNED_COMMISSION_RATE = 0.2; // 司机自备车辆抽成20%
    
    public double calculate(Order order) {
        double baseFare = 0;
        double distanceFare = 0;
        double timeFare = 0;
        double tolls = 0;

        switch (order.getType()) {
            case ECONOMY:
                // 基础费为8元，距离费1.8元/公里，时间费0.3元/分钟
                baseFare = 8 + (order.getDistance() > 3 ? (order.getDistance() - 3) * 1.5 : 0);
                distanceFare = order.getDistance() * 1.8;
                timeFare = order.getDuration() * 0.3;
                break;
            case LUXURY:
                // 基础费为15元，距离费3.5元/公里，时间费0.5元/分钟
                baseFare = 15 + (order.getDistance() > 5 ? (order.getDistance() - 5) * 2.5 : 0);
                distanceFare = order.getDistance() * 3.5;
                timeFare = order.getDuration() * 0.5;
                break;
            case IS_AUTONOMOUS:
                // 基础费为8元，距离费1.8元/公里，时间费0.3元/分钟
                // 自动驾驶车辆收费为经济型车辆的30%
                baseFare = 8 * AUTONOMOUS_DISCOUNT;
                distanceFare = order.getDistance() * 1.8 * AUTONOMOUS_DISCOUNT;
                timeFare = order.getDuration() * 0.3 * AUTONOMOUS_DISCOUNT;
                break;
            case CARPOOL:
                // 拼车费用分摊逻辑
                break;
        }
        //长途费
        if (order.getDistance() > 20) {
            distanceFare *= 1.2;
        }

        return baseFare + distanceFare + timeFare + tolls;
    }
    
    /**
     * 计算公司抽成金额
     * @param order 订单
     * @param vehicle 车辆
     * @return 抽成金额
     */
    public double calculateCommission(Order order, Vehicle vehicle) {
        double totalFare = calculate(order);
        double commissionRate;
        
        if (vehicle.isCompanyOwned()) {
            // 公司车辆抽成30%
            commissionRate = COMPANY_OWNED_COMMISSION_RATE;
        } else {
            // 司机自备车辆抽成20%
            commissionRate = DRIVER_OWNED_COMMISSION_RATE;
        }
        
        return totalFare * commissionRate;
    }
}    